<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="ip地址" prop="ip">
        <el-input
          v-model="queryParams.ip"
          placeholder="请输入用户ip地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="评论人地区" prop="location">
        <el-input
          v-model="queryParams.location"
          placeholder="请输入ip坐落地区"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="评论人" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          placeholder="请输入评论人名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:hairComment:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:hairComment:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:hairComment:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:hairComment:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="hairCommentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="评论id" align="center" prop="id" v-if="true"/>
      <el-table-column label="父评论id" align="center" prop="parentId" />
      <el-table-column label="贴子id" align="center" prop="postId" />
      <el-table-column label="评论人id" align="center" prop="userId" />
      <el-table-column label="评论内容" align="center" prop="content" />
      <el-table-column label="点赞数" align="center" prop="likes" />
      <el-table-column label="评论类型" align="center" prop="typeString" />
      <el-table-column label="用户ip地址" align="center" prop="ip" />
      <el-table-column label="ip坐落地区" align="center" prop="location" />
      <el-table-column label="评论人头像" align="center" prop="avatar">
        <template #default="scope">
          <template v-if="scope.row.avatar != null">
            <template v-for="item in scope.row.avatar.split(',')" :key="item">
              <image-preview :src="item" :width="50" :height="50" />
            </template>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="评论人名称" align="center" prop="nickname" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:hairComment:edit']">修改</el-button>
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:hairComment:remove']">删除</el-button>
          </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改极客侠小程序评论对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="hairCommentRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="父评论id" prop="parentId">
          <el-input v-model="form.parentId" placeholder="请输入父评论id" />
        </el-form-item>
        <el-form-item label="贴子id" prop="postId">
          <el-input v-model="form.postId" placeholder="请输入贴子id" />
        </el-form-item>
        <el-form-item label="评论人id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入评论人id" />
        </el-form-item>
        <el-form-item label="评论内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="点赞数" prop="likes">
          <el-input v-model="form.likes" placeholder="请输入点赞数" />
        </el-form-item>
        <el-form-item label="用户ip地址" prop="ip">
          <el-input v-model="form.ip" placeholder="请输入用户ip地址" />
        </el-form-item>
        <el-form-item label="ip坐落地区" prop="location">
          <el-input v-model="form.location" placeholder="请输入ip坐落地区" />
        </el-form-item>
        <el-form-item label="评论人头像" prop="avatar">
          <el-input v-model="form.avatar" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="评论人名称" prop="nickname">
          <el-input v-model="form.nickname" placeholder="请输入评论人名称" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="HairComment">
import { listHairComment, getHairComment, delHairComment, addHairComment, updateHairComment } from "@/api/system/hairComment";

const { proxy } = getCurrentInstance();

const hairCommentList = ref([]);
const open = ref(false);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    parentId: undefined,
    postId: undefined,
    userId: undefined,
    content: undefined,
    likes: undefined,
    type: undefined,
    ip: undefined,
    location: undefined,
    avatar: undefined,
    nickname: undefined,
  },
  rules: {
    id: [
      { required: true, message: "评论id不能为空", trigger: "blur" }
    ],
    parentId: [
      { required: true, message: "父评论id不能为空", trigger: "blur" }
    ],
    postId: [
      { required: true, message: "贴子id不能为空", trigger: "blur" }
    ],
    userId: [
      { required: true, message: "评论人id不能为空", trigger: "blur" }
    ],
    content: [
      { required: true, message: "评论内容不能为空", trigger: "blur" }
    ],
    likes: [
      { required: true, message: "点赞数不能为空", trigger: "blur" }
    ],
    type: [
      { required: true, message: "评论类型：评论类型字符串不能为空", trigger: "change" }
    ],
    ip: [
      { required: true, message: "用户ip地址不能为空", trigger: "blur" }
    ],
    location: [
      { required: true, message: "ip坐落地区不能为空", trigger: "blur" }
    ],
    avatar: [
      { required: true, message: "评论人头像不能为空", trigger: "blur" }
    ],
    nickname: [
      { required: true, message: "评论人名称不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询极客侠小程序评论列表 */
function getList() {
  loading.value = true;
  listHairComment(queryParams.value).then(response => {
    hairCommentList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    parentId: null,
    postId: null,
    userId: null,
    content: null,
    likes: null,
    type: null,
    ip: null,
    location: null,
    avatar: null,
    nickname: null,
    delFlag: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("hairCommentRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加极客侠小程序评论";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  loading.value = true
  reset();
  const _id = row.id || ids.value
  getHairComment(_id).then(response => {
    loading.value = false;
    form.value = response.data;
    open.value = true;
    title.value = "修改极客侠小程序评论";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["hairCommentRef"].validate(valid => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id != null) {
        updateHairComment(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      } else {
        addHairComment(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除极客侠小程序评论编号为"' + _ids + '"的数据项？').then(function() {
    loading.value = true;
    return delHairComment(_ids);
  }).then(() => {
    loading.value = true;
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  }).finally(() => {
    loading.value = false;
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/hairComment/export', {
    ...queryParams.value
  }, `hairComment_${new Date().getTime()}.xlsx`)
}

getList();
</script>
